﻿package ascript.components.input
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import starling.events.KeyboardEvent;
	import ascript.GameComponent;
	import ascript.GameObject;
	
	//#########################
	// InputBase Class
	//#########################
	public class InputBase extends GameComponent
	{
		//=========================
		// Variables
		//=========================	
		// Privates
		private var inputGameObject:GameObject; // pointer to owned game object
		private var inputGame:Game; // pointer to game
		
		//=========================
		// Constructors
		//=========================
		public function InputBase()
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Default Attributes
			defaultAttribute( tObject, "InputIsEnabled", true );
			
			// Create pointer to owned object
			inputGameObject = tObject;
			inputGame = tGame;
			
			// Add Input Listeners
			( tObject.stage ).addEventListener( KeyboardEvent.KEY_DOWN, keyboardPressInput );
			( tObject.stage ).addEventListener( KeyboardEvent.KEY_UP, keyboardReleaseInput );
		}
		
		protected function keyboardPressInput( tEvent:KeyboardEvent ):void
		{
			if ( inputGameObject.getAttribute( "InputIsEnabled" ) )
			{
				executeInput( inputGame, inputGameObject, tEvent.keyCode, true );
			}
		}
		
		protected function keyboardReleaseInput( tEvent:KeyboardEvent ):void
		{
			if ( inputGameObject.getAttribute( "InputIsEnabled" ) )
			{
				executeInput( inputGame, inputGameObject, tEvent.keyCode, false );
			}
		}
		
		protected function executeInput( tGame:Game, tObject:GameObject, tKeyCode:uint, tIsPressed:Boolean ):void
		{
		}
		
		override public function removeComponent( tGame:Game, tObject:GameObject ):void
		{
			// Remove Input Listeners
			tObject.stage.removeEventListener( KeyboardEvent.KEY_DOWN, keyboardPressInput );
			tObject.stage.removeEventListener( KeyboardEvent.KEY_DOWN, keyboardReleaseInput );
		}
	}
}
